package com.one.blocks.rbac.util;

import lombok.experimental.UtilityClass;
import org.apache.commons.collections4.CollectionUtils;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static com.one.blocks.rbac.constant.RbacConstant.PERMS_SPLIT;

/**
 * @author linfuxin Created on 2022-07-04 14:32:13
 */
@UtilityClass
public class PermissionHelper {

    public static Set<String> listPermsToPermsSet(List<String> perms) {
        if (CollectionUtils.isEmpty(perms)) {
            return Collections.emptySet();
        }
        // perms可能是多个权限用逗号分割的组合,这里进行转换
        Set<String> data = new HashSet<>(perms.size());
        for (String perm : perms) {
            if (perm.contains(PERMS_SPLIT)) {
                data.addAll(Arrays.stream(perm.split(PERMS_SPLIT)).collect(Collectors.toSet()));
            } else {
                data.add(perm);
            }
        }
        return data;
    }
}
